﻿using System;
using System.Net;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeanceEngine
{
    //Black holes
    public class BlackHole : SVMapObject
    {
        //constructor
        public BlackHole(SVGame game)
            : base(game)
        {
            Game.BlackHoles.Add(this);
        }

        //removal
        public override void Remove()
        {
            base.Remove();
            Game.BlackHoles.Remove(this);
        }

        //display name
        public override string DisplayName
        {
            get
            {
                return "Black Hole";
            }
        }

        //Radius
        public int Radius
        {
            get
            {
                return _radius;
            }
            set
            {
                _radius = value;
            }
        }

        //Does the black hole pull in the specified object?
        public bool PullsInObject(SVMapObject svm, int engines)
        {
            if (DistanceQuick(svm) <= Radius * 2)
            {
                double distance = svm.Distance(this);
                if (distance <= Radius)
                {
                    double chance = ((Radius - distance) * 100.0d) / (double)Radius;
                    chance -= engines;
                    chance *= 0.4d;
                    if (SVGame.Random(1, 100) < chance)
                        return true;
                }
            }
            return false;
        }

        //persistence
        public override string Persist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.Tokenize(Radius);
            return base.Persist() + tok.Result;
        }
        public override SVTokenizer Parse(string s)
        {
            SVTokenizer tok = base.Parse(s);
            Radius = tok.TokenInt;
            return tok;
        }

        //private members
        private int _radius = 10;
        private BlackHoleInfo _info = null;

        //Access the info object for brains
        internal BlackHoleInfo Info
        {
            get
            {
                if (_info == null)
                    _info = new BlackHoleInfo(this);
                return _info;
            }
        }
    }
}
